package com.atguigu.gmall;

import com.atguigu.gmall.common.constant.RedisConst;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * @author: atguigu
 * @create: 2023-04-15 14:02
 */
@SpringBootApplication   //如果扫描不到持久层mapper,持久层对象无法产生
@EnableDiscoveryClient
@EnableAsync  //开启SpringBoot异步 默认情况使用内置线程池对象 一定要使用自定义线程池
public class ProductApp implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ProductApp.class);
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            //优雅关闭时候,执行发送移除nacos实例信息
            System.out.println("服务停止,执行一些业务");
        }));
    }


    @Autowired
    private RedissonClient redissonClient;

    /**
     * 应用启动后执行,初始化布隆过滤器
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        System.out.println("springboot应用启动后执行");
        //1.创建布隆过滤器
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        //2.为布隆过滤器指定数据规模,误判率
        bloomFilter.tryInit(100000, 0.03);
    }
}
